<template>
  <a-modal
    :title="'AI判定规则' + title"
    width='1000px'
    :visible='visible'
    :maskClosable='false'
    :confirmLoading='confirmLoading'
    @cancel='handleCancel'
    cancelText='关闭'>
    <template slot='footer'>
      <a-button @click='handleCancel'>取消</a-button>
      <a-button @click='handleOk' type='primary' v-if='!disableSubmit'>确认</a-button>
    </template>
    <a-spin :spinning='confirmLoading'>
      <a-form :form='form'>
        <a-row :gutter='24'>
          <a-divider style="font-weight: 600;margin-top: 20px;" orientation="left">基础信息</a-divider>
          <a-col :md='12' :sm='24'>
            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='规则名称'>
              <a-input placeholder='请输入规则名称' :disabled='disableSubmit'
                       v-decorator="['aiRuleName',{rules: [{ required: true, message: '请输入规则名称' }]}]" />
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24'>
            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='有效时间'
              >
              <a-range-picker :disabled='disableSubmit' format='YYYY-MM-DD' :disabledDate='disabledDate'
                              v-decorator="['rangDate',{rules: [{ required: true, message: '请选择时间' }]}]" />
            </a-form-item>
          </a-col>
          <a-col :md='19' :sm='24'>
            <a-form-item
              :labelCol='{xs:4,xm:24}'
              :wrapperCol='{xs:19,xm:24}'
              label='备注'
              >
              <a-textarea placeholder='请输入备注' :disabled='disableSubmit' v-decorator="['remarks']" />
            </a-form-item>
          </a-col>
          <a-divider style="font-weight: 600;margin-top: 20px;" orientation="left">判定规则(满足以下条件ai自动判断为合格)</a-divider>
          <a-col :md='12' :sm='24'>
            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='识别度'>
              <a-input-number :max='1' :min='0' placeholder='请输入最低识别度(0~1)' :disabled='disableSubmit'
                           style='width:100%'   v-decorator="['resolution',{rules: [{ required: true, message: '请输入最低识别度' }]} ]" />
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24'>
            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='陈列行数'>
              <a-input placeholder='请输入最低陈列行数' :disabled='disableSubmit'
                       v-decorator="['displayLine',{rules: [{ required: true, message: '请输入最低陈列行数' }]}]" />
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24'>
            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='陈列列数'>
              <a-input placeholder='请输入最低陈列列数' :disabled='disableSubmit'
                       v-decorator="['displayList',{rules: [{ required: true, message: '请输入最低陈列列数' }]}]" />
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24'>
            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='陈列数量'>
              <a-input placeholder='请输入最低陈列数量' :disabled='disableSubmit'
                       v-decorator="['displayNum',{rules: [{ required: true, message: '请输入最低陈列数量' }]}]" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>

  </a-modal>
</template>

<script>
import { getAction, httpAction, postAction } from '@/api/manage'
import { pick } from 'lodash'
import JTreeSelect from '@/components/cn-form/JTreeSelect'
import JAreaLinkage from '@comp/cn-form/CnAreaLinkage'
import UploadImage from '@/components/upload-image/UploadImage'
import JImageUpload from '@/components/cn-form/JImageUpload'
import JSelectSubjects from '@/components/cn-select-popup/JSelectSubjects'
import JSelectCust from '@/components/cn-select-popup/CnSelectCust'
import JSelectProduct from '@/components/cn-select-popup/JSelectProduct'
import moment from 'moment'

export default {
  name: 'terminal-modal',
  data() {
    return {
      disableSubmit: false,
      type: 1,
      disabled: false,
      detailPage: false,
      title: '操作',
      visible: false,
      model: {},
      ptQueryParam: {},

      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      subOp: [],
      yearOp: [],
      quarterOp: [],
      monthOp: [],
      addOrUpdateUrl: window._CONFIG['domianTpmURL'] + '/tpmAiRule/saveOrUpdate',
      validatorRules: {
        deleteStatus: {
          rules: [{
            required: true, message: '请输入值', trigger: 'submit'
          }]
        }
      }
    }
  },
  components: { JTreeSelect, JAreaLinkage, UploadImage, JImageUpload, JSelectSubjects, JSelectCust, JSelectProduct },

  created() {
    // // 生成预算年度
    // let nowYear=new Date().getFullYear()
    // let op=[]
    // for (let i=0;i<=10;i++){
    //   op.push({name:nowYear+i,value:nowYear+i})
    // }
    // this.yearOp=op
    let quarter = [
      {
        name: '一季度',
        value: 1
      },
      {
        name: '二季度',
        value: 2
      },
      {
        name: '三季度',
        value: 3
      },
      {
        name: '四季度',
        value: 4
      }
    ]

    this.quarterList = quarter
    // let monthList={
    //   1:[
    //     {
    //       name:'01',
    //       value:'01'
    //     },{
    //       name:'02',
    //       value:'02'
    //     },{
    //       name:'03',
    //       value:'03'
    //     }],
    //   2:[
    //     {
    //       name:'04',
    //       value:'04'
    //     },{
    //       name:'05',
    //       value:'05'
    //     },{
    //       name:'06',
    //       value:'06'
    //     }],
    //   3:[
    //     {
    //       name:'07',
    //       value:'07'
    //     },{
    //       name:'08',
    //       value:'08'
    //     },{
    //       name:'09',
    //       value:'09'
    //     }],
    //   4:[
    //     {
    //       name:'10',
    //       value:'10'
    //     },{
    //       name:'11',
    //       value:'11'
    //     },{
    //       name:'12',
    //       value:'12'
    //     }],
    // }
    //
    // this.monthList=monthList
    // // 生成预算月度
    let month = [
      {
        name: '01',
        value: '01'
      }, {
        name: '02',
        value: '02'
      }, {
        name: '03',
        value: '03'
      }, {
        name: '04',
        value: '04'
      }, {
        name: '05',
        value: '05'
      }, {
        name: '06',
        value: '06'
      }, {
        name: '07',
        value: '07'
      }, {
        name: '08',
        value: '08'
      }, {
        name: '09',
        value: '09'
      }, {
        name: '10',
        value: '10'
      }, {
        name: '11',
        value: '11'
      }, {
        name: '12',
        value: '12'
      }]
    this.monthList = month
    // postAction(window._CONFIG['domianTpmURL']+ "/budget/tpmBudgetSubjects/findPageList",{current:1,size:100}).then(res=>{
    //   if(res.success){
    //     let op=[]
    //     res.result.records.map(v=>{
    //       op.push({name:v.subjectsName,value:v.subjectsCode})
    //     })
    //     this.subOp=op
    //   }
    // })


  },

  watch: {
    // 'model.productLevelName'(val){
    //     this.handleQueryParms()
    // }

  },
  methods: {
    // handleQueryParms(){
    //
    // },
    disabledDate(current) {
      return current < moment().startOf('day')
    },
    add() {
      this.edit({})
    },
    edit(record, status) {
      this.type = 1
      this.disabled = false
      this.form.resetFields()
      this.visible = true
      this.model = {}
      if (record.id) {
        this.model = Object.assign({}, record)
        this.$nextTick(() => {
          this.form.setFieldsValue({ rangDate: [this.model.startDate, this.model.endDate] })
          this.form.setFieldsValue(pick(this.model, Object.keys(this.model)))
        })
      }

    },

    close() {
      this.visible = false
      this.$emit('close')
    },
    handleCancel() {
      this.close()
      this.model = {}
    },
    handleOk() {
      const that = this
      this.form.validateFields((err, values) => {
        if (!err) {
          this.confirmLoading = true
          let formData = Object.assign({}, this.model, values)
          if (formData.rangDate[0]._d) {
            formData.startDate = formData.rangDate[0].format('YYYY-MM-DD')
            formData.endDate = formData.rangDate[1].format('YYYY-MM-DD')
          } else if (formData.rangDate[0] && !formData.rangDate[0]._d) {
            formData.startDate = formData.rangDate[0]
            formData.endDate = formData.rangDate[1]
          }
          postAction(this.addOrUpdateUrl, formData).then(res => {
            if (!res.success) {
              that.$message.error(res.message)
              that.confirmLoading = false
              return
            }
            that.confirmLoading = false
            that.$emit('ok')
            that.handleCancel()
            that.$message.success(res.message)
          })
        }
      })
    },
    changeSelect(options, name, event) {
      name = name.split('Code')
      if (name.length > 1) {
        name = name[0] + 'Name'
        let item = options.find(item => item.value == event)
        this.model[name] = item.name
      }
    },
    selectRole2(e1, e2) {
      if (e2 == 'productName') {
        this.form.setFieldsValue({
          productCode: e1[0].matnr,
          productName: e1[0].maktx
        })
      } else if (e2 == 'budgetSubjectsCode') {
        this.form.setFieldsValue({
          budgetSubjectsCode: e1[0].subjectsCode,
          budgetSubjectsName: e1[0].subjectsName
        })
      }
    },
    selectRole(row, name, key) {
      console.log(row)
      let value = row.map(v => v[key])
      name = name.split('Code')
      if (name.length >= 1) {
        name = name[0] + 'Name' + (name[1] ? name[1] : '')
        this.model[name] = value + ''
      }
    },

    // getYearQuMonth(key) {
    //   let _this=this
    //
    //   this.$nextTick(() => {
    //     if(key==1){
    //       _this.quarterOp=[]
    //       _this.monthOp=[]
    //       this.form.resetFields(['budgetQuarter'])
    //       this.form.resetFields(['budgetMonth'])
    //     }
    //     if(key==2){
    //       this.form.resetFields(['budgetMonth'])
    //       _this.monthOp=[]
    //     }
    //
    //
    //     let year=this.form.getFieldValue('budgetYear')
    //     let quarter=this.form.getFieldValue('budgetQuarter')
    //     postAction(this.getYear,{bgQuarter:quarter,bgYear:year}).then(res=>{
    //       if(res.success){
    //         let list=[]
    //
    //         if(!year&&!quarter){
    //           if(res.result.length>0){
    //             _this.yearOp=res.result
    //           }else {
    //             _this.$message.info('请先增加预算财年')
    //           }
    //           return
    //         }
    //         if(year&&!quarter){
    //           res.result.map(v=>{
    //             let item = _this.quarterList.find(item=>item.value==v)
    //             if(item){
    //               list.push(item)
    //             }
    //           })
    //
    //
    //           _this.quarterOp=list
    //         }
    //         if(year&&quarter){
    //           res.result.map(v=>{
    //             let item = _this.monthList.find(item=>item.value==v)
    //             if(item){
    //               list.push(item)
    //             }
    //           })
    //
    //
    //           _this.monthOp=list
    //         }
    //
    //       }
    //     })
    //   })
    // },

    changeJselect(name, value) {
      if (name == 'productLevelCode') {
        this.form.resetFields('productCode')
        this.model.productName = ''
        this.$nextTick(() => {
          this.ptQueryParam = { typeCode: this.form.getFieldValue('productLevelCode') }
        })
      }
      name = name.split('Code')
      if (name.length > 1) {
        name = name[0] + 'Name' + (name[1] ? name[1] : '')
        this.model[name] = value
      }
    },
    budgetType(e, v) {
      this.type = e
    },
    changeSelectQuarter(e) {
      this.monthOp = this.monthList[e]
    }
  }
}
</script>

<style scoped>
.gonghuoxinxi {
  width: 100%;
  border-bottom: 1px dashed black;
  margin: 10px 0;
  padding: 15px 5px;
  display: flex;
  flex-direction: column;
}

/deep/ .ant-modal-body {
  padding: 0px;
}

</style>
